package yrl.fileReadWrite;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class ObjectStreamDemo {
	protected static Logger logger = LogManager.getLogger(ObjectInputStreamDemo.class.getName());

	public static void writeObject(Person person, File file) {
		FileOutputStream fos;
		try {
			fos = new FileOutputStream(file);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(person);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void readObject(Person person, File file) {
		FileInputStream fis;
		try {
			fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			person = (Person) ois.readObject();
			person.name = "lucy";
			logger.info(person);
			ois.close();
			fis.close();
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file = new File("word.txt");
		Person person = new Person();
		person.age = 12;
		person.name = "yrl";
		person.country = "China";
		ObjectStreamDemo.writeObject(person,file);
		person.name="Tom";//没有起效
		person.age=0;//没有起效
		person.country="American";
		ObjectStreamDemo.readObject(person,file);
	}

}
